/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Etbl_Car_bookings;
import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Mr.aUgust
 */
public class DALtbl_Car_bookings extends BaseConnection{
    CallableStatement clbStm=null;
    ResultSet rs=null;
      public boolean addtbl_Car_bookings(Etbl_Car_bookings obj){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Car_bookings_add(?,?,?,?,?,?,?,?,?,?,?)}");
            clbStm.setDate("startDate", obj.getStartDate());                                                     
            clbStm.setDate("endDate", obj.getEndDate());                                                     
            clbStm.setDate("dateReturn", obj.getDateReturn());
            clbStm.setBoolean("driver",obj.isDriver());
            clbStm.setFloat("price", obj.getPrice());                                                     
            clbStm.setFloat("refund", obj.getRefund());  
            clbStm.setString("createOn", obj.getCreateOn());  
            clbStm.setString("modifiedOn", obj.getModifiedOn());  
            clbStm.setInt("status", obj.getStatus());  
            clbStm.setInt("carId", obj.getCarId());  
            clbStm.setInt("userId", obj.getUserId());  
            clbStm.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Car_bookings.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      
      public boolean edittbl_Car_bookings(Etbl_Car_bookings obj){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Car_bookings_update(?,?,?,?,?,?,?,?,?,?,?)}");
            clbStm.setInt("carbookings_Id", obj.getCarbookings_Id());  
            clbStm.setDate("startDate", obj.getStartDate());                                                     
            clbStm.setDate("endDate", obj.getEndDate());                                                     
            clbStm.setDate("dateReturn", obj.getDateReturn());
            clbStm.setBoolean("driver",obj.isDriver());
            clbStm.setFloat("price", obj.getPrice());                                                     
            clbStm.setFloat("refund", obj.getRefund());  
            clbStm.setString("createOn", obj.getCreateOn());  
            clbStm.setString("modifiedOn", obj.getModifiedOn());  
            clbStm.setInt("status", obj.getStatus());  
            clbStm.setInt("carId", obj.getCarId());  
            clbStm.setInt("userId", obj.getUserId());                                    
            clbStm.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Car_bookings.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      
      public boolean deletetbl_Car_bookings(int carbookings_Id){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Car_bookings_delete(?)}");
            clbStm.setInt("carbookings_Id",carbookings_Id);              
            clbStm.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Car_bookings.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
      }
      public Etbl_Car_bookings getOne_Carbooking(int carBookingId){
          Etbl_Car_bookings oCarbooking=new Etbl_Car_bookings();
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Car_bookings_get(?)}");
            clbStm.setInt("carbookings_Id", carBookingId);
            rs=clbStm.executeQuery();
            if(rs.next()){
                oCarbooking.setCarbookings_Id(carBookingId);
                oCarbooking.setCreateOn(rs.getString("createOn"));
                oCarbooking.setDateReturn(rs.getDate("dateReturn"));
                oCarbooking.setDriver(rs.getBoolean("driver"));
                oCarbooking.setEndDate(rs.getDate("endDate"));
                oCarbooking.setModifiedOn(rs.getString("modifiedOn"));
                oCarbooking.setNameCar(rs.getString("CarName"));
                oCarbooking.setPrice(rs.getFloat("price"));
                oCarbooking.setRefund(rs.getFloat("refund"));
                oCarbooking.setStartDate(rs.getDate("startDate"));
                oCarbooking.setStatus(rs.getInt("status"));
                oCarbooking.setUserName(rs.getString("UserName"));
                oCarbooking.setUserId(rs.getInt("userId"));
                oCarbooking.setCarId(rs.getInt("carId"));
            }
            clbStm.close();
            rs.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Car_bookings.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            return oCarbooking;
        }
      }
      public ArrayList<Etbl_Car_bookings> getListCarbookings(String orderBy, String ASCorDESC, String WhereClause, int PagingIndex, int PagingSize){
        ArrayList<Etbl_Car_bookings> arrCarbookings=new ArrayList<Etbl_Car_bookings>();
          try {
            getConnection();
            clbStm=cnn.prepareCall("{call tbl_Car_bookings_paged(?,?,?,?,?)}");
              clbStm.setString("OrderBy", orderBy);
              clbStm.setString("TangGiam", ASCorDESC);
              clbStm.setString("WhereClause", WhereClause);
              clbStm.setInt("PageIndex", PagingIndex);
              clbStm.setInt("PageSize", PagingSize);
              rs=clbStm.executeQuery();
              DALTotalPageIndex.getTotalPage("tbl_Car_bookings", WhereClause, PagingSize);
              while(rs.next()){
                  Etbl_Car_bookings oCarbooking=new Etbl_Car_bookings();
                oCarbooking.setCarbookings_Id(rs.getInt("carbookings_Id"));
                oCarbooking.setCreateOn(rs.getString("createOn"));
                oCarbooking.setDateReturn(rs.getDate("dateReturn"));
                oCarbooking.setDriver(rs.getBoolean("driver"));
                oCarbooking.setEndDate(rs.getDate("endDate"));
                oCarbooking.setModifiedOn(rs.getString("modifiedOn"));
                oCarbooking.setNameCar(rs.getString("CarName"));
                oCarbooking.setPrice(rs.getFloat("price"));
                oCarbooking.setRefund(rs.getFloat("refund"));
                oCarbooking.setStartDate(rs.getDate("startDate"));
                oCarbooking.setStatus(rs.getInt("status"));
                oCarbooking.setUserName(rs.getString("UserName"));
                oCarbooking.setUserId(rs.getInt("userId"));
                oCarbooking.setCarId(rs.getInt("carId"));
                arrCarbookings.add(oCarbooking);
            }
            clbStm.close();
            rs.close();
            releaseConnection();
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Car_bookings.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return arrCarbookings;
        }
      }
      public float CalRefund_Carbooking(int carbookingId, Date deteRefund){
        try {
            getConnection();
            clbStm=cnn.prepareCall("{call CarBooking_Refund_sp(?,?)}");
            clbStm.setInt("carBookingId", carbookingId);
            clbStm.setDate("dateRefund", deteRefund);
            rs=clbStm.executeQuery();
            if(rs.next())
                return rs.getFloat("refund");
            else
                return 0;
        } catch (SQLException ex) {
            Logger.getLogger(DALtbl_Car_bookings.class.getName()).log(Level.SEVERE, null, ex);
            return 0;
        }
      }
}
